package com.hqj;

import com.spider.domain.Movie;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SpiderYangGuang {

    public static void main(String[] args) {
        test();
    }


    public static List<Object> test() {
        String uri = "http://www.weather.com.cn/weather1d/101010100.shtml#search";
        String html = sendUri(uri);
        List<Object> li = getContent(html);
        List<Object> list = new ArrayList<>();
        for(Object obj : li){
            String nodeHtml = sendUri(obj+"");
            getNodeContent(nodeHtml,list);
        }
        return list;
    }


    public static String sendUri(String uri){
        List<Object> list = new ArrayList<>();
        //生成一个httpclient,相当于打开一个浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //设置访问超时时间
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();
        CloseableHttpResponse response = null;
        String html = null;
        HttpGet request = new HttpGet(uri);
        try{

            request.setConfig(requestConfig);
            //执行get请求，相当于输入地址，回车访问
            response = httpClient.execute(request);
            //判断访问状态码 200为成功，404为访问失败
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                //获取响应内容
                HttpEntity httpEntity = response.getEntity();
                html = EntityUtils.toString(httpEntity,"utf-8");
                System.out.println(html);
            }else{
                System.out.println("页面访问失败，错误代码:"+response.getStatusLine().getStatusCode());
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //关闭
            HttpClientUtils.closeQuietly(response);
            HttpClientUtils.closeQuietly(httpClient);
        }

        return html;
    }





    public static List<Object> getContent(String html){
        List<Object> list = new ArrayList<>();
        //把字符串转换为doc对象
        Document document = Jsoup.parse(html);
        String rootPath = "";
        Elements elements1 = document.select(".path");
        for(Element element:elements1){
            Element a = element.select("ul").first().select("a").first();
            rootPath = a.attr("href");
        }
        Elements elements = document.select(".tbspan");
        for(Element element:elements){
            Element tr = element.select("tr").get(1);
            Element td = tr.select("td").get(1);
            Element a =  td.select("a").first();
            String text = a.text();//获取出标签的内容
            String href = a.attr("href");
            list.add(rootPath + href);
        }

        return list;

    }


    public static void getNodeContent(String html,List<Object> list){

        //把字符串转换为doc对象
        Document document = Jsoup.parse(html);
        String rootPath = "";
        Elements elements = document.select("#Zoom");

        for(Element element:elements){
            Element span = element.select("span").first();
            Element img = span.select("img").first();
            String imgSrc = img.attr("src");
            String text = span.text();

            String texts[] = text.split("◎");
            String chName = texts[1].replace("译　　名　","");
            String engName = texts[2].replace("片　　名　","");
            String year = texts[3].replace("年　　代　","");
            Movie movie = new Movie();
            movie.setMovieName(chName);
            movie.setEngName(engName);
            movie.setYear(year);
            movie.setTime(texts[4].replace("类　　别　",""));
            movie.setComefrom("阳光电影");
            //movie.setMember(texts[14].replace("主　　演　",""));
            list.add(movie);
        }

    }



}
